package ui;

import control.*;
import control.bean.ControlPropertiesBean;
import control.bean.ControlBeanConstant;
import javax.swing.*;
import javax.swing.border.CompoundBorder;
import java.awt.*;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: Deshun
 * Date: 2/13/13
 * Time: 3:46 PM
 * To change this template use File | Settings | File Templates.
 */
public class FormArea extends JComponent{

    java.util.List<Control> controlList;
    FormPanel formPanel;
    JPanel listPanel;
    JPanel propertiesPanel;

    public FormArea() {
        formPanel = new FormPanel(this);
        listPanel = new JPanel(new BorderLayout());
        listPanel.setOpaque(false);
        propertiesPanel = new JPanel();
        controlList = new ArrayList<Control>();
    }

    public Component buildFormArea() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setOpaque(false);

        JPanel panel2 = new JPanel(new BorderLayout());
        panel2.setOpaque(false);

        JScrollPane scrollPane = new JScrollPane(buildList());
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        scrollPane.setBorder(new CompoundBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6), scrollPane.getBorder()));

        panel2.add(scrollPane, BorderLayout.CENTER);
        panel2.add(new JSeparator(SwingConstants.VERTICAL), BorderLayout.EAST);

        panel.add(panel2, BorderLayout.CENTER);
        panel.add(buildProperties(), BorderLayout.EAST);

        return panel;
    }

    Component buildList() {
        if (listPanel.getComponentCount() > 0) {
            listPanel.removeAll();
        }

        //TODO use row to make header instead
//        panel.add(new Header().buildControl(), BorderLayout.NORTH);
        listPanel.add(formPanel.explode(), BorderLayout.CENTER);

        return listPanel;
    }

    Component buildProperties() {
        if ( propertiesPanel.getComponentCount() > 0 ) {
            propertiesPanel.removeAll();
        }
        propertiesPanel.setOpaque(false);
        propertiesPanel.setPreferredSize(new Dimension(180, 150));
        propertiesPanel.setMinimumSize(new Dimension(180, 150));
//        panel.add(Box.createHorizontalStrut(180), BorderLayout.SOUTH);
        return propertiesPanel;
    }

    void addControl(int rowNumber, Control control) {
        control.setFormArea(this);
        formPanel.addControl(rowNumber, control);
        controlList.add(control);
        revalidate();
    }

    //its not removing the correct item
    public void removeControl(int rowNumber, Control control) {
        formPanel.removeControl(rowNumber, control);
        controlList.remove(control);
        formPanel.explode();
        formPanel.getPanel().revalidate();
        formPanel.getPanel().repaint();
    }

    public void showProperties(Component properties) {
        buildProperties();
        propertiesPanel.add(properties, BorderLayout.CENTER);
        propertiesPanel.revalidate();
        propertiesPanel.repaint();
    }

    public void hideProperties() {
        buildProperties();
        propertiesPanel.revalidate();
        propertiesPanel.repaint();
    }

    Component requestToolbar(int rowID) {
        ItemToolbar itemToolbar = new ItemToolbar(this, rowID);
        return itemToolbar.itemToolbarPanel();
    }

    public java.util.List<ControlPropertiesBean> exportForm() {
        java.util.List<ControlPropertiesBean> beanList = new ArrayList<ControlPropertiesBean>();

        for ( Control currentControl : controlList ) {
            beanList.add(currentControl.exportData());
        }

        return beanList;
    }

    public void importForm(java.util.List<ControlPropertiesBean> beanList) {
        //clear everything and reload from scratch
        formPanel.getRows().clear();
        formPanel.addRow();
        //TODO look thru the adding process to make sure addition is possible WITHOUT adding in order
        for ( ControlPropertiesBean currentBean : beanList ) {
            int rowNumber = Integer.parseInt(currentBean.getProperty(ControlBeanConstant.ROWNUMBER));

            while ( formPanel.getRows().size()-1 != rowNumber ) {
                formPanel.addRow();
            }

            Control control = null;

            switch ( Integer.parseInt(currentBean.getProperty(ControlBeanConstant.FIELDTYPE)) ) {
                case ControlBeanConstant.TEXTFIELD:
                    control = new Textfield();
                    break;
                case ControlBeanConstant.MULTITEXTFIELD:
                    control = new MultiLineTextfield();
                    break;
                case ControlBeanConstant.DATEPICKER:
                    control = new DatePicker();
                    break;
                case ControlBeanConstant.TIMEPICKER:
                    control = new TimePicker();
                    break;
                case ControlBeanConstant.RADIO:
                    control = new Radio();
                    break;
                case ControlBeanConstant.CHECKBOX:
                    control = new CheckBox();
                    break;
                case ControlBeanConstant.DROPDOWNLIST:
                    control = new Dropdown();
                    break;
                case ControlBeanConstant.LISTBOX:
                    control = new Listbox();
                    break;
                case ControlBeanConstant.MATRIX:
                    break;
                case ControlBeanConstant.FILEATTACHMENT:
                    break;
                case ControlBeanConstant.IMAGE:
                    break;
                default:
                    control = null;
                    break;
            }

            try {
                addControl(rowNumber, control);
                control.importData(currentBean);
            } catch ( Exception e ) {
                System.out.println("Control import error @ FormArea.importForm()");
                e.printStackTrace();
            }
        }

        formPanel.explode();
        formPanel.getPanel().revalidate();
        formPanel.getPanel().repaint();
    }

}
